package cc.geguo.ognl;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

import org.junit.Test;
/**
 * ognl 核心api:OgnlContext 类
 * */
public class OgnlDemo1 {

	/**
	 * @throws Exception
	 */
	@Test
	public void testOgnl() throws Exception {
		// 构建对象
		OgnlContext context = new OgnlContext();

		User user = new User();
		user.setId(100);
		user.setName("Jack");
//		添加值
		context.put("user", user);
		
//		解析表达式
		Object ognl = Ognl.parseExpression("#user.name");
//		取出表达式中的值
		Object value = Ognl.getValue(ognl, context, context.getRoot());
		
		System.out.println(value);
	}
	
	/**
	 * @throws Exception
	 * 通过setRoot 方式设置值
	 * 表达式中不需要写 #
	 */
	@Test
	public void testOgn2() throws Exception {
		OgnlContext context = new OgnlContext();
		User user = new User();
		user.setId(100);
		user.setName("Jack");
		context.setRoot(user);// 访问 user 对象的属性
		Object ognl = Ognl.parseExpression("address.province");
		Object value = Ognl.getValue(ognl, context, context.getRoot());
		
		System.out.println(value);
	}
	
	/**
	 * @throws Exception
	 * 解析 静态方法的调用
	 */
	@Test
	public void testOgn3() throws Exception {
		OgnlContext context = new OgnlContext();
		Object ognl = Ognl.parseExpression("@@floor(10.9)");
		Object value = Ognl.getValue(ognl, context, context.getRoot());
		System.out.println(value);
	}
}





